﻿using System;
using System.Collections;
using System.Data;
using CE.Database;


/// <summary>
/// Summary description for CarNCDFactorDB
/// </summary>
/// 
namespace CE.Provider
{
    public class CarNCDFactorDB
    {
        public CarNCDFactorDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }


        public Int64 Insert(CarNCDFactor CarNCDFactor)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "[sprocProviderCarNCDFactorInsertUpdateSingleItem]";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@ProviderCarNCDFactorId", CarNCDFactor.ProviderCarNCDFactorId);
            _sPara.Add("@ProviderCompanyId", CarNCDFactor.ProviderCompanyId);
            _sPara.Add("@NCD", CarNCDFactor.NCD);
            _sPara.Add("@Rate", CarNCDFactor.Rate);
            _sPara.Add("@RefCEUserId", CarNCDFactor.RefCEUserId);
            _sPara.Add("@Status", CarNCDFactor.Status);
            _sPara.Add("@FInfo1", CarNCDFactor.FInfo1);
            _sPara.Add("@DInfo1", CarNCDFactor.DInfo1);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public CarNCDFactorList GetProviderCarNCDFactorList(Int64 ProviderCompanyId)
        {
            CarNCDFactorList _CarNCDFactorlist = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocProviderCarNCDFactorSelectItemList";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCompanyId", ProviderCompanyId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _CarNCDFactorlist = new CarNCDFactorList();
                    for (int i = 0; i < _dt.Rows.Count; i++)
                    {
                        CarNCDFactor _CarNCDFactor = new CarNCDFactor();
                        _CarNCDFactor = FillDataRecord(_dt, i);
                        _CarNCDFactorlist.Add(_CarNCDFactor);
                    }
                }
            }
            return _CarNCDFactorlist;
        }

        public CarNCDFactor GetProviderCarNCDFactor(Int64 ProviderCompanyId, Int32 ProviderNCDFactorId)
        {
            CarNCDFactor _vehicleyear = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocProviderCarNCDFactorSelectSingleItem";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCompanyId", ProviderCompanyId);
            _spara.Add("@ProviderCarNCDFactorId", ProviderNCDFactorId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _vehicleyear = new CarNCDFactor();
                    _vehicleyear = FillDataRecord(_dt, 0);
                }
            }
            return _vehicleyear;
        }

        public CarNCDFactor GetProviderCarNCDFactor(Int64 ProviderNCDFactorId)
        {
            CarNCDFactor _vehicleyear = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocProviderCarNCDFactorSelectSingleItem";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCarNCDFactorId", ProviderNCDFactorId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _vehicleyear = new CarNCDFactor();
                    _vehicleyear = FillDataRecord(_dt, 0);
                }
            }
            return _vehicleyear;
        }

        private CarNCDFactor FillDataRecord(DataTable myDataTable, int RowId)
        {
            CarNCDFactor myCarNCDFactor = new CarNCDFactor();
            myCarNCDFactor.ProviderCarNCDFactorId = Convert.ToInt64(myDataTable.Rows[RowId]["ProviderCarNCDFactorId"].ToString());
            myCarNCDFactor.ProviderCompanyId = Convert.ToInt64(myDataTable.Rows[RowId]["ProviderCompanyId"].ToString());
            myCarNCDFactor.NCD = myDataTable.Rows[RowId]["NCD"].ToString();
            myCarNCDFactor.Rate = Convert.ToSingle(myDataTable.Rows[RowId]["Rate"].ToString());
            myCarNCDFactor.RefCEUserId = Convert.ToInt64(myDataTable.Rows[RowId]["RefCEUserId"].ToString());
            myCarNCDFactor.Status = Convert.ToBoolean(myDataTable.Rows[RowId]["Status"].ToString());
            myCarNCDFactor.ModifiedDate = Convert.ToDateTime(myDataTable.Rows[RowId]["ModifiedDate"].ToString());

            if (myDataTable.Rows[RowId]["FInfo1"].ToString() != "")
                myCarNCDFactor.FInfo1 = Convert.ToSingle(myDataTable.Rows[RowId]["FInfo1"].ToString());

            if (myDataTable.Rows[RowId]["DInfo1"].ToString() != "")
                myCarNCDFactor.DInfo1 = Convert.ToDateTime(myDataTable.Rows[RowId]["DInfo1"].ToString());
            else
                myCarNCDFactor.DInfo1 = Convert.ToDateTime(myDataTable.Rows[RowId]["ModifiedDate"].ToString());

            return myCarNCDFactor;
        }
    }
}
